Page({
    data: {
      currentWeek: 1,
      weekRange: Array.from({length: 20}, (_, i) => `第${i + 1}周`),
      weekdays: ['周一', '周二', '周三', '周四', '周五'],
      timeSlots: [
        { start: '8:00', end: '9:40' },
        { start: '10:00', end: '11:40' },
        { start: '14:00', end: '15:40' },
        { start: '16:00', end: '17:40' },
        { start: '19:00', end: '20:40' }
      ],
      courses: []
    },
  
    onLoad: function() {
      this.loadCourses();
    },
  
    onShow: function() {
      this.loadCourses();
    },
  
    loadCourses: function() {
      const that = this;
      wx.request({
        url: `${getApp().globalData.baseUrl}/api/courses`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        },
        data: {
          week: that.data.currentWeek
        },
        success(res) {
          if (res.data.success) {
            that.setData({
              courses: res.data.courses
            });
          }
        }
      });
    },
  
    onWeekChange: function(e) {
      this.setData({
        currentWeek: parseInt(e.detail.value) + 1
      });
      this.loadCourses();
    },
  
    getCourse: function(timeIndex, dayIndex) {
      return this.data.courses.find(course => 
        course.timeIndex === timeIndex && 
        course.dayIndex === dayIndex
      );
    },
  
    getCourseClass: function(timeIndex, dayIndex) {
      const course = this.getCourse(timeIndex, dayIndex);
      return course ? 'has-course' : '';
    },
  
    showCourseDetail: function(e) {
      const { time, day } = e.currentTarget.dataset;
      const course = this.getCourse(time, day);
      if (course) {
        wx.showModal({
          title: course.name,
          content: `教室：${course.location}\n教师：${course.teacher}\n班级：${course.className}`,
          showCancel: false
        });
      }
    }
  });